<?php
require_once("includes/include.php");
$Helper = new Helper();
$Log = new Log();
class uploadPictures{
	public $Log;
	public $Helper;
	public function __construct(){
		$this->Log = new Log();
		$this->Helper = new Helper();
	}
	public function uploadIt(){
		$tempFile = $_FILES['Filedata']['tmp_name'];
		$postFolderArr = explode("/",$_REQUEST['folder']);
		$length = count($postFolderArr)-1;
		$postFolder = $postFolderArr[$length];
		$baseFolder = ($this->Helper->getPicturceBaseFolder()).$postFolder."/";
		$this->Log->write("this time basefolder:".$baseFolder);
		if (!is_dir($baseFolder)){
			mkdir($baseFolder);
		}
		date_default_timezone_set("Asia/shanghai");
		$tempDate = date("Y_m_d_H_i_s").rand(10000,99999);
		$subx= strrchr($_FILES['Filedata']['name'],".");
		$targetFile = $baseFolder.$tempDate.$subx;
		$this->Log->write("this time file path:".$targetFile);
		
		$move = move_uploaded_file($tempFile,$targetFile);
		
		echo $targetFile;
		if ($move){
			@session_start();
			$dao = new picturesdao();
			$add = $dao->addPicture($_FILES['Filedata']['name'],$targetFile,$postFolder);
			$coverOld = $dao->getPictureFolderCover($postFolder);
			if (!$coverOld){
				$dao->setPictureFolderCover($postFolder,$targetFile);
			}
			if ($add){
				$this->Log->write("success add pic");
			}else {
				$this->Log->write("failed add pic");
			}
		}
	}
}
$upload = new uploadPictures();
if(!empty($_FILES)){
	$upload->uploadIt();
}else{
	echo "no files";
}
?>